package cn.yjr.vector;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.yjr.vector.tools.MD5;
import cn.yjr.vector.tools.SharedPreferencesHelper;
import cn.yjr.vector.tools.YJRHttpClient;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

public class WorkPeriodActivity extends SherlockActivity implements OnClickListener{

	LayoutInflater inflater;
	ActionBar actionBar;
	View title_bar;
	Context context;
	String response;
	JSONObject jsonInfo;
	String workPeriod;
	SharedPreferencesHelper spHelper;
	RelativeLayout week_rl, day_rl, type_rl;
	TextView week_tv, day_tv, type_tv, save_tv;
	String[] weeks = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
	String[] days = {"上午","下午"};
	String[] types = {"普通门诊","专家门诊","特需门诊"};
	String[] a = {"P","Z","T"};
	String[] b = {"A","B"};
	int week = 0;
	int day = 0;
	int type = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_work_period);
		inflater = this.getLayoutInflater();
		actionBar = getSupportActionBar();
		context = this;
		this.spHelper = new SharedPreferencesHelper(this.context);
		initTitleBar();
		
		initComponent();
		
		initEventListener();
		
		
		
	
	}
	
	private void initTitleBar(){
		ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.WRAP_CONTENT,Gravity.CENTER);
		title_bar = inflater.inflate(R.layout.work_period_action_bar, null);
		
		actionBar.setCustomView(title_bar,lp);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
	}
	private void initComponent(){
		week_rl  = (RelativeLayout) findViewById(R.id.week_rl);
		day_rl  = (RelativeLayout) findViewById(R.id.day_rl);
		type_rl  = (RelativeLayout) findViewById(R.id.type_rl);

		week_tv = (TextView) findViewById(R.id.week_tv);
		day_tv = (TextView) findViewById(R.id.day_tv);
		type_tv = (TextView) findViewById(R.id.type_tv);
		
		save_tv = (TextView) findViewById(R.id.save);
	}
	
	private void initEventListener(){
		week_rl.setOnClickListener(this);
		day_rl.setOnClickListener(this);
		type_rl.setOnClickListener(this);
		save_tv.setOnClickListener(this);

		
	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.week_rl:
			showWeekPicker();
			break;
		case R.id.day_rl:
			showDayPicker();
			break;
		case R.id.type_rl:
			showTypePicker();
			break;
		case R.id.save:
			workPeriod = "-" + (1+week) + b[day] + a[type];
			update();
			break;
		default:
				break;
		}
		
	}
	
	private void update(){
		RequestParams params = new RequestParams(); 
		params.add("workPeriod", workPeriod);
		YJRHttpClient.post(context,"/doctor/updateInfo", params, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {  
				 Toast.makeText(context, data.toString(), Toast.LENGTH_SHORT).show();
	             	jsonInfo = data;
	             	try {
						if(jsonInfo.getInt("result") == 1){
							YJRHttpClient.setCookie(context); 
							Toast.makeText(context, "添加成功", Toast.LENGTH_LONG).show();
							Intent intent = new Intent();
							intent.putExtra("week", weeks[week]);
							intent.putExtra("day", days[day]);
							intent.putExtra("type",	types[type]);
							((Activity) context).setResult(Activity.RESULT_OK,intent);
							((Activity) context).finish();
							
						}else{
							Toast.makeText(context, jsonInfo.getString("info"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }  
		});
	}
	

	private void showWeekPicker() {
		Builder builder=new AlertDialog.Builder(context);
	    builder.setTitle("选择出诊时间");
	    builder.setSingleChoiceItems(weeks, 0,new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				week = which;
				week_tv.setText(weeks[week]);
			}
	    	
	    });
	    builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				week = which;
				dialog.dismiss();
			}
	    	
	    });
	    builder.setNegativeButton("取消", null);
	    AlertDialog dialog=builder.create();
	    dialog.show();
	}
	
	private void showTypePicker() {
		Builder builder=new AlertDialog.Builder(context);
	    builder.setTitle("选择出诊类型");
	    builder.setSingleChoiceItems(types, 0,new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				type = which;
				
			}
	    	
	    });
	    builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				type_tv.setText(types[type]);
				dialog.dismiss();
			}
	    	
	    });
	    builder.setNegativeButton("取消", null);
	    AlertDialog dialog=builder.create();
	    dialog.show();
	}
	private void showDayPicker() {
		Builder builder=new AlertDialog.Builder(context);
	    builder.setTitle("选择出诊时间段");
	    builder.setSingleChoiceItems(days, 0,new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				day = which;
			}
	    	
	    });
	    builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				day_tv.setText(days[day]);
				dialog.dismiss();
			}
	    	
	    });
	    builder.setNegativeButton("取消", null);
	    AlertDialog dialog=builder.create();
	    dialog.show();
	}
}